//
// Created by 19854 on 23/04/30.
//

#include "DS3231Task.h"

TaskHandle_t ds3231TaskHand;
extern TaskHandle_t OLEDTaskHand;

void ds3231Task(void *ptParam)
{
    RTC_DS3231 rtc;
    char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

    if (! rtc.begin()) {
        Serial.println("Couldn't find RTC");
        Serial.flush();
        while (1) delay(10);
    }

    while (true)
    {
        DateTime now = rtc.now();
//        Serial.print(now.year(), DEC);
//        Serial.print('/');
//        Serial.print(now.month(), DEC);
//        Serial.print('/');
//        Serial.print(now.day(), DEC);
//        Serial.print(" (");
//        Serial.print(daysOfTheWeek[now.dayOfTheWeek()]);
//        Serial.print(") ");
//        Serial.print(now.hour(), DEC);
//        Serial.print(':');
//        Serial.print(now.minute(), DEC);
//        Serial.print(':');
//        Serial.print(now.second(), DEC);
//        Serial.println();
//
//        Serial.print(" since midnight 1/1/1970 = ");
//        Serial.print(now.unixtime());
//        Serial.print("s = ");
//        Serial.print(now.unixtime() / 86400L);
//        Serial.println("d");

        // calculate a date which is 7 days, 12 hours, 30 minutes, 6 seconds into the future
//        DateTime future (now + TimeSpan(7,12,30,6));
//
//        Serial.print(" now + 7d + 12h + 30m + 6s: ");
//        Serial.print(future.year(), DEC);
//        Serial.print('/');
//        Serial.print(future.month(), DEC);
//        Serial.print('/');
//        Serial.print(future.day(), DEC);
//        Serial.print(' ');
//        Serial.print(future.hour(), DEC);
//        Serial.print(':');
//        Serial.print(future.minute(), DEC);
//        Serial.print(':');
//        Serial.print(future.second(), DEC);
//        Serial.println();

        Serial.print("Temperature: ");
        Serial.print(rtc.getTemperature());
        Serial.println(" C");
        xTaskNotify(OLEDTaskHand, now.unixtime(), eSetValueWithOverwrite);
        Serial.println();
        vTaskDelay(1000);
    }
}